package com.weiyirong99.crm.app.server;

import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.blankj.utilcode.util.ActivityUtils;
import com.weiyirong99.crm.app.ui.ClientDetailActivity;

import org.json.JSONObject;

import java.util.List;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.helper.Logger;

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "MyReceiver";

    private NotificationManager nm;


    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();
        Logger.e(TAG, "onReceive - " + intent.getAction() + ", extras: 111 ");

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Logger.e(TAG, "JPush 用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Logger.e(TAG, "接受到推送下来的自定义消息");

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Logger.e(TAG, "接受到推送下来的通知");

            receivingNotification(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Logger.e(TAG, "用户点击打开了通知");

            openNotification(context, bundle);

        } else {
            Logger.e(TAG, "Unhandled intent - " + intent.getAction());
        }
    }

    private void receivingNotification(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        Logger.e(TAG, " title : " + title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        Logger.e(TAG, "message : " + message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Logger.e(TAG, "extras : " + extras);
//         ["data":"(("customerId ": "232154 ",("mobile":("13251077554\",("companyId ":("3 ")" "type":"MAKE_CALL")
        try {
            JSONObject extrasJson = new JSONObject(extras);
            String type = extrasJson.optString("type");
            Log.e("WG", "receivingNotification: " + type);
            if (type.equals("MAKE_CALL")) {
                String data = extrasJson.optString("data");
                JSONObject jsonObject = new JSONObject(data);
                Log.e("WG", "receivingNotification: " + jsonObject);
                String mobile = jsonObject.optString("mobile");

                String companyId = jsonObject.optString("companyId");
                String customerId = jsonObject.optString("customerId");
                int id = Integer.parseInt(customerId);

//                String topActivityByActivityManager = getTopActivityByActivityManager(context);
//                Log.e("WG", "receivingNotification: " + topActivityByActivityManager);
                if (ActivityUtils.getTopActivity() instanceof ClientDetailActivity){
                    Intent sendIntent=new Intent("ClientDetailActivity");
                    //发送广播，将被Activity组件中的BroadcastReceiver接收
                    sendIntent.putExtra("customer_id",id);
                    sendIntent.putExtra("phone", mobile);
//                    sendIntent.putExtra("current",current);
                    context.sendBroadcast(sendIntent);
                }else {
                    Log.e("WG", "receivingNotification: " + mobile);
                    Intent mIntent = new Intent(context, ClientDetailActivity.class);
                    mIntent.putExtra("phone", mobile);
                    mIntent.putExtra("customer_id", id);
                    mIntent.putExtra("isTuiSong", "1");
                    mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(mIntent);
                }

//                context.startActivity(intent);
            }

        } catch (Exception e) {
            Log.e("WG", "receivingNotification: " + e.toString());
            Logger.e(TAG, "Unexpected: extras is not a valid json" + e);
            return;
        }
    }

    private void openNotification(Context context, Bundle bundle) {
//        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String extras = "";
        String myValue = "";
        try {
            JSONObject extrasJson = new JSONObject(extras);
            myValue = extrasJson.optString("myKey");
        } catch (Exception e) {
            Logger.e(TAG, "Unexpected: extras is not a valid json" + e);
            return;
        }
//        if (TYPE_THIS.equals(myValue)) {
//            Intent mIntent = new Intent(context, ThisActivity.class);
//            mIntent.putExtras(bundle);
//            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(mIntent);
//        } else if (TYPE_ANOTHER.equals(myValue)) {
//            Intent mIntent = new Intent(context, AnotherActivity.class);
//            mIntent.putExtras(bundle);
//            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(mIntent);
//        }
    }

    //    public static class MyBroadcast extends BroadcastReceiver{
//
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            int control =intent.getIntExtra("control",-1);
//            //广播通知Activity更改图标、文本框
//
//        }
//    }
//    private String getTopActivityByActivityManager(Context context) {
//        ActivityManager activityManager = (ActivityManager) (context
//                .getSystemService(android.content.Context.ACTIVITY_SERVICE));
//        List<ActivityManager.RunningTaskInfo> listTask = activityManager.getRunningTasks(0);
//        String activityName = "";
//        if (listTask != null && !listTask.isEmpty()) {
//            ActivityManager.RunningTaskInfo runningTaskInfo = listTask.get(1);
//            activityName = runningTaskInfo.topActivity.getClassName();
//        }
//        return activityName;
//    }
}
